package com.whq.thrift.api.gateway.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.gateway.db.MerchantApiPermission;
import com.whq.thrift.api.gateway.db.MerchantApiPermissionMapper;
import com.whq.thrift.api.gateway.db.MerchantKey;
import com.whq.thrift.api.gateway.db.MerchantKeyMapper;
import com.whq.thrift.api.gateway.db.MethodInfo;
import com.whq.thrift.api.gateway.db.MethodInfoMapper;
import com.whq.thrift.api.gateway.service.VerifyDbService;

@Service
public class VerifyDbServiceImpl implements VerifyDbService {
	// merchant => key
	public static final Map<String, String> MAP_MARCHANT_NAME_KEY = new HashMap<String, String>(0);
	// merchantName_apiMethod => MerchantApiPermission
	public static final Map<String, MerchantApiPermission> MAP_MARCHANT_API_PERMISSION = new HashMap<String, MerchantApiPermission>(0);
	// method => method info
	public static final Map<String, MethodInfo> MAP_METHOD_INFO = new HashMap<String, MethodInfo>(0);
	
	@Autowired
	private MerchantApiPermissionMapper merchantApiPermissionMapper;
	
	@Autowired
	private MerchantKeyMapper merchantKeyMapper;
	
	@Autowired
	private MethodInfoMapper methodInfoMapper;
	
	public void getDataFromDbToCache() {
		getMethodInfoFromDb();
		getMerchantInfoFromDb();
		getMerchantApiPermissionFromDb();
	}
	
	private void getMerchantInfoFromDb() {
		List<MerchantKey> merchantList = merchantKeyMapper.findAllRecords();
		if ( (merchantList != null) && (!merchantList.isEmpty())) {
			MAP_MARCHANT_NAME_KEY.clear();
			
			for (Iterator<MerchantKey> iterator = merchantList.iterator(); iterator.hasNext();) {
				MerchantKey merchantKey = (MerchantKey) iterator.next();
				if (merchantKey != null) {
					MAP_MARCHANT_NAME_KEY.put(merchantKey.getMerchantName(), merchantKey.getSignatureKey());
				}
			}
		}
	}
	
	private void getMerchantApiPermissionFromDb() {
		List<MerchantApiPermission> merchantApiPermissionList = merchantApiPermissionMapper.findAllMerchantApiPermissionList();
		if ( (merchantApiPermissionList != null) && (!merchantApiPermissionList.isEmpty())) {
			MAP_MARCHANT_API_PERMISSION.clear();
			
			for (Iterator<MerchantApiPermission> iterator = merchantApiPermissionList.iterator(); iterator.hasNext();) {
				MerchantApiPermission merchantApiPermission = (MerchantApiPermission) iterator.next();
				if (merchantApiPermission != null) {
					String merchantNameApiMethod = merchantApiPermission.getMerchantName() + "_" + merchantApiPermission.getApiMethod();
					MAP_MARCHANT_API_PERMISSION.put(merchantNameApiMethod, merchantApiPermission);
				}
			}
		}
	}

	private void getMethodInfoFromDb() {
		List<MethodInfo> methodList = methodInfoMapper.findAllValidMethods();
		if ( (methodList != null) && (!methodList.isEmpty())) {
			MAP_METHOD_INFO.clear();
			
			for (Iterator<MethodInfo> iterator = methodList.iterator(); iterator.hasNext();) {
				MethodInfo methodInfo = (MethodInfo) iterator.next();
				if (methodInfo != null) {
					MAP_METHOD_INFO.put(methodInfo.getMethodName(), methodInfo);
				}
			}
		}
		
	}
	
	public String getMerchantSignatureKey(String merchantName) {
		if (MAP_MARCHANT_NAME_KEY.isEmpty()) {
			getMerchantInfoFromDb();
		}
		return MAP_MARCHANT_NAME_KEY.get(merchantName);
	}
	
	public boolean chkMerchantApiPermissionValid(String merchantName, String apiMethod, String ip) {
		if (MAP_MARCHANT_API_PERMISSION.isEmpty()) {
			getMerchantApiPermissionFromDb();
		}
		String merchantNameApiMethod = merchantName + "_" + apiMethod;
		MerchantApiPermission merchantApiPermission = MAP_MARCHANT_API_PERMISSION.get(merchantNameApiMethod);
		if (merchantApiPermission == null) {
			return false;
		}
		// TODO: 以后增加IP判断
		return true;
	}
	
	public MethodInfo getMethodInfo(String apiMethod) {
		if (MAP_METHOD_INFO.isEmpty()) {
			getMethodInfoFromDb();
		}
		return MAP_METHOD_INFO.get(apiMethod);
	}

}
